/**
 * Created by thanatoptions on 15-9-10.
 */

/**
 * 分类
  * @param options
 * @constructor
 */
var Category = function (options){
    //this._id = options && options._id || null
    this.name = options && options.name || null
    this.parent = options && options.parent || null
    this.children = options && options.children || [] //List<Category>
    this.ctgattr = options && options.ctgattr || [] //List<Category.Attribute>
}

/**
 * 分类属性
 * @param options
 * @constructor
 */
Category.Attribute = function (options) {
    //this._id = options && options._id || null
    this.name = options && options.name || null
    this.values = options && options.values || null
    this.type = options && options.type || 2 //0-->文本框，1-->复选框，2-->下拉框，3-->单选框
    this.inputWidth = options && options.inputWidth || 100
    this.order = options && options.order || 0
}

/**
 * 商品
 * @param options
 * @constructor
 */
var Goods = function (options) {
    //this._id = options && options._id || null
    this.title = options && options.title || null
    this.brand = options && options.brand || null
    this.label = options && options.label || null
    this.store = options && options.store || null
    this.publishTime = options && options.publishTime || new Date().getTime() //发布时间
    this.keywords = options && options.keywords || []
    this.images = options && options.images || []
    this.price = options && options.price || null
    this.comment = options && options.comment || 0
    this.category = options && new Category(options.category) || null
    this.catalog = options && new Goods.Catalog(options.catalog) || null //商品自定义的属性选择 >选择颜色 >选择尺码 >...
    this.attributes = options && options.attributes || [] //[{name: '', value: '' or []}, {...}, ...]
}
//inner class
Goods.Catalog = function (options) {
    this.name = options && options.name || null
    this.parent = options && options.parent || null
    this.attributes = options && options.attributes || [] //List<Goods.Catalog.Attribute>
}
//inner class
Goods.Catalog.Attribute = function (options) {
    this.name = options && options.name || null
    this.image = options && options.image || null
    this.repertory = options && options.repertory || 0 //库存
}

/**
 * 选择器，暂时不支持多选
 * @param options
 * @constructor
 */
var Selector = function (options) {
    //field
    var $self = this
    $self.attributes = options && options.attributes || null // 属性选择器 {String : String}
    $self.selected = options && new Selector.Selected(options.selected) || new Selector.Selected()//已选择属性

    //合并每个goods的属性，只留下共有的属性
    $self.merge = function (attr) {
        if(!$self.attributes){
            $self.attributes = {}
            for(var key in attr){
                if(attr[key].split('，').length>1)
                    $self.attributes[key] = []
                else
                    $self.attributes[key] = [attr[key]]
            }
        }else{
            for(var key in attr){
                if($self.attributes.hasOwnProperty(key)){
                    [].concat(attr[key]).forEach(function (v) {
                        if(v.split('，').length>1){
                            return
                        }

                        if($self.attributes[key].indexOf(v)==-1){
                            $self.attributes[key].push(v)
                        }
                    })
                }else{
                    delete $self.attributes[key]
                }
            }
        }
    }

    //属性过滤
    $self.resort = function () {
        if($self.attributes){

            for(var key in $self.attributes){

                var filtered = []
                $self.attributes[key].forEach(function (item) {
                    var re001 = /其它.+/
                    if(re001.test(item)){
                        return
                    }
                    var re002 = /其他.*/
                    if(re002.test(item)){
                        return
                    }

                    for(var i in $self.attributes[key]){
                        if(item != $self.attributes[key][i] && item.startsWith($self.attributes[key][i])){
                            return
                        }
                    }

                    filtered.push(item)
                })

                $self.attributes[key] = filtered

                //>>>删除属性值数目少于3的属性
                if($self.attributes[key].length<3){
                    delete $self.attributes[key]
                    continue
                }

                //>>>升序排序
                $self.attributes[key].sort().reverse()

                //>>>将'其它'移步到最后
                var index001 = $self.attributes[key].indexOf('其它')
                if(index001 != -1){
                    $self.attributes[key].splice(index001, 1)
                    $self.attributes[key].push('其它')
                }

            }
        }
    }
}

//已选择属性
Selector.Selected = function (options) {
    var $self = this
    $self.price = options && options.price || null
    $self.sort = options && options.sort || null
    $self.category = options && options.category || null //分类选择器 {name:String, id:Long, brother:[], child:{} }
    $self.attributes = options && options.attributes || {} //已选择的属性

    //在URL参数中剔除出指定的属性
    $self.reject = function (key) {
        var params = []
        for(var k in $self.attributes){
            if(k != key){
                params.push('selected.attributes.'+k+'='+$self.attributes[k])
            }
        }
        return params.join('&')
    }

    //在URL参数中添加指定的属性
    $self.append = function (key, value) {
        var params = []
        for(var k in $self.attributes){
            params.push('selected.attributes.'+k+'='+$self.attributes[k])
        }
        params.push('selected.attributes.'+key+'='+value)
        return params.join('&')
    }

    //生成URL
    $self.origin = function () {
        var params = []
        for(var k in $self.attributes){
            params.push('selected.attributes.'+k+'='+$self.attributes[k])
        }
        return params.join('&')
    }
}

/**
 * 分页对象
 * @param pageNum
 * @param pageSize
 * @param recordCount
 * @param data
 * @constructor
 */
var Page = function (pageNum, pageSize, recordCount, data) {
    var $self = this
    var PAGE_NAVIGATE = 5
    if(arguments.length>4){
        console.log('bad constructor!!')
        return
    }
    $self.pageNum = pageNum || 1
    $self.pageSize = pageSize || 25
    $self.recordCount = recordCount || 0
    $self.data = data || []
    $self.pageCount = parseInt(($self.recordCount + $self.pageSize - 1) / $self.pageSize)
    var HALF_PAGE_NAVIGATE = parseInt(PAGE_NAVIGATE/2)

    if ($self.pageCount <= PAGE_NAVIGATE){
        $self.begin = 1;
        $self.end = $self.pageCount;
    }else if ($self.pageNum <= HALF_PAGE_NAVIGATE){
        $self.begin = 1;
        $self.end = PAGE_NAVIGATE;
    }else if ($self.pageNum + HALF_PAGE_NAVIGATE > $self.pageCount){
        $self.begin = $self.pageCount - (PAGE_NAVIGATE - 1);
        $self.end = $self.pageCount;
    }else {
        $self.begin = $self.pageNum - HALF_PAGE_NAVIGATE;
        $self.end = $self.pageNum + HALF_PAGE_NAVIGATE;
    }
}


module.exports = {
    Category: Category,
    Goods: Goods,
    Selector: Selector,
    Page: Page
}